import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'dart:ui' as ui;

import 'package:flutter/services.dart';

class TestSvgPath extends StatefulWidget {
  @override
  _TestSvgPathState createState() => _TestSvgPathState();
}

class _TestSvgPathState extends State<TestSvgPath> {
  ui.Image? _image;

  void _loadImage() async {
    _image = await loadImageFromAssets('assets/images/user/user_wx_icon.png');
    setState(() {});
  }

  Future<ui.Image>? loadImageFromAssets(String path) async {
    ByteData data = await rootBundle.load(path);
    return decodeImageFromList(data.buffer.asUint8List());
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _loadImage();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('模板'),
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: _image == null
            ? SizedBox.shrink()
            : CustomPaint(
                painter: DrawImgPaint(image: _image!),
              ),
      ),
    );
  }
}

class DrawImgPaint extends CustomPainter {
  DrawImgPaint({required this.image});

  ui.Image image;

  @override
  void paint(Canvas canvas, Size size) {
    print(size);
    Paint paint = Paint();
    // TODO: implement paint
    canvas.translate(100, 100);
    canvas.drawImageRect(
        image,
        Rect.fromCenter(
            center: Offset(image.width / 2, image.height / 2),
            width: 100,
            height: 100),
        const Rect.fromLTRB(0, 0, 100, 100),
        paint);

    _drawSmallIcon(canvas, paint);
  }

  _drawSmallIcon(Canvas canvas, Paint paint) {
    canvas.drawImageRect(
        image,
        Rect.fromCenter(
            center: Offset(image.width / 2, image.height / 2),
            width: 100,
            height: 100),
        const Rect.fromLTRB(25, 25, 75, 75),
        paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}
